package com.white.algebra.controller;

import com.white.algebra.domain.Pager;
import com.white.algebra.domain.Role;
import com.white.algebra.domain.Syslog;
import com.white.algebra.domain.UserInfo;
import com.white.algebra.service.UserService;
import com.white.algebra.service.impl.UserServiceImpl;
import com.white.algebra.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class UserManageController extends BaseServlet{
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        UserService userService=new UserServiceImpl();
        int page= WebUtils.parseInt(req.getParameter("page"),1);
        int size= WebUtils.parseInt(req.getParameter("size"),4);
        if(page<1){
            page=1;
        }
        String fuzzyName=req.getParameter("fuzzyName");
        int startRecord=(page-1)*size;
        List<UserInfo> userList = userService.findAll(startRecord,size,fuzzyName);
        int recordCount=userService.getLogNum(fuzzyName);
        Pager<UserInfo> pager=new Pager<>();
        pager.calculate(userList,page,size,recordCount);
        req.setAttribute("page",pager);
        req.setAttribute("fuzzyName",fuzzyName);
        req.getRequestDispatcher("/pages/user-list.jsp").forward(req,resp);
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        UserService userService=new UserServiceImpl();
        String id=req.getParameter("id");
        UserInfo user = userService.findById(id);
        List<Role> role = userService.findNotRoleByUserId(id);
        req.setAttribute("user",user);
        req.setAttribute("role",role);
        req.getRequestDispatcher("/pages/user-show.jsp").forward(req,resp);
    }
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        UserService userService=new UserServiceImpl();
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        String phoneNum=req.getParameter("phoneNum");
        int status= WebUtils.parseInt(req.getParameter("status"),0);
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo.setEmail(email);
        userInfo.setPhoneNum(phoneNum);
        userInfo.setStatus(status);
        userService.save(userInfo);
        resp.sendRedirect(req.getContextPath()+"/userManage?action=findAll");
    }
    protected void relatedRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        UserService userService=new UserServiceImpl();
        String userId=req.getParameter("userId");
        String roleId=req.getParameter("roleId");
        userService.relatedRole(userId,roleId);
        resp.sendRedirect(req.getHeader("Referer"));
    }
    protected void deleteRoleById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        UserService userService=new UserServiceImpl();
        String userId=req.getParameter("userId");
        String id=req.getParameter("roleId");
        userService.deleteRoleById(id,userId);
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
